package cumeo89.chii.client.form;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import cumeo89.chii.client.Chii;
import cumeo89.chii.client.mw.Page;
import cumeo89.chii.client.ui.TristateCheckBox;
import cumeo89.chii.client.util.AsyncCallback;

public class SavingDialog extends DialogBox {

	private static final String DEFAULT_SUMMARY = "<Tóm lược sửa đổi>";
	private Button btnSave;
	private Button btnCancel;
	private HTML htmPreview;
	private Page page;
	private boolean saved;
	private TristateCheckBox chkMinor = new TristateCheckBox("Nhỏ");
	private TristateCheckBox chkWatch = new TristateCheckBox("Theo&nbsp;dõi");

	public SavingDialog() {
		setText("Lưu sửa đổi");

		final VerticalPanel pnlContainer = new VerticalPanel();
		setWidget(pnlContainer);
		pnlContainer.setSize("100%", "100%");

		final Label lblPreview = new Label("Xem thử");
		pnlContainer.add(lblPreview);

		final ScrollPanel scrPreview = new ScrollPanel();
		pnlContainer.add(scrPreview);
		scrPreview.setStyleName("chii-preview");
		scrPreview.setSize("450px", "250px");

		htmPreview = new HTML("");
		scrPreview.setWidget(htmPreview);
		htmPreview.setSize("100%", "100%");

		final HorizontalPanel pnlSummary = new HorizontalPanel();
		pnlContainer.add(pnlSummary);
		pnlSummary.setSpacing(3);
		pnlSummary.setWidth("100%");

		final TextBox txtSummary = new TextBox();
		pnlSummary.add(txtSummary);
		txtSummary.addFocusHandler(new FocusHandler() {
			public void onFocus(FocusEvent event) {
				if (DEFAULT_SUMMARY.equals(txtSummary.getText())) {
					txtSummary.setText("");
				}
			}
		});
		txtSummary.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				if (txtSummary.getText().trim().isEmpty()) {
					txtSummary.setText(DEFAULT_SUMMARY);
				}
			}
		});
		pnlSummary.setCellWidth(txtSummary, "100%");
		txtSummary.setWidth("100%");
		txtSummary.setText(DEFAULT_SUMMARY);

		pnlSummary.add(chkMinor);
		pnlSummary.add(chkWatch);

		final HorizontalPanel pnlButton = new HorizontalPanel();
		pnlContainer.add(pnlButton);
		pnlContainer.setCellHorizontalAlignment(pnlButton,
				HasHorizontalAlignment.ALIGN_CENTER);
		pnlButton.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		pnlButton.setSpacing(3);

		btnSave = new Button();
		pnlButton.add(btnSave);
		btnSave.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saved = false;
				btnSave.setEnabled(false);
				btnCancel.setEnabled(false);

				if (DEFAULT_SUMMARY.equals(txtSummary.getText())) {
					page.setSummary("");
				} else {
					page.setSummary(txtSummary.getText());
				}
				if (chkMinor.isChanged()) {
					page.setMinor(chkMinor.isChecked());
				}
				if (chkMinor.isChanged()) {
					page.setWatch(chkWatch.isChecked());
				}

				Chii.get().getMediaWiki().save(page,
						new AsyncCallback<Void>() {

							public void onSuccess(Void result) {
								saved = true;
								hide();
							}

							public void onFailure(Throwable caught) {
								Window.alert("Có lỗi: " + caught.getMessage());
							}
						});
			}
		});
		btnSave.setText("Lưu");

		btnCancel = new Button();
		pnlButton.add(btnCancel);
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saved = false;
				hide();
			}
		});
		btnCancel.setText("Thôi");
	}

	public void setPage(Page page) {
		if (page == null) {
			throw new IllegalArgumentException();
		}
		this.page = page;
		htmPreview.setHTML("Đang tải nội dung...");
		Chii.get().getMediaWiki().preview(page,
				new AsyncCallback<String>() {

					public void onSuccess(String result) {
						htmPreview.setHTML(result);
					}

					public void onFailure(Throwable caught) {
						htmPreview
								.setHTML("Có lỗi xảy ra! Không thể xem thử mục từ!");
					}
				});
		chkMinor.resetState();
		chkWatch.resetState();
		btnCancel.setEnabled(true);
		btnSave.setEnabled(true);
	}

	public Page getPage() {
		return page;
	}

	public boolean isSaved() {
		return saved;
	}

}
